<!--
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-01-17 13:49:57
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-05-06 11:22:56
 * @Description:
 *
 * Copyright (c) 2024 by error: git config user.name & please set dead value or install git, All Rights Reserved.
-->
<template>
  <div class="document_wrap">
    <div class="system_list_wrap">
      <div class="system_list">
        <div
          v-for="(item, index) in systemList"
          :key="index"
          class="system_item"
        >
          <div class="system_item_content">
            <div class="system_item_close" @click="systemDeleteClick(item)">
              <i class="el-icon-close" />
            </div>
            {{ item.groupName }}
          </div>
          <div class="system_item_operate">
            <div class="operate_item" style="border-right: 1px solid #eee" @click="systemDocumenyJump(item)">
              文档
            </div>
            <div class="operate_item" @click="systemItemClick(item)">菜单</div>
          </div>
        </div>
        <div class="system_item new_system_item">
          <div style="display: flex; flex-direction: column; align-items: center; cursor: pointer;" @click="editSystemInputVisible = true">
            <div style="font-size: 22px">
              <i class="el-icon-circle-plus-outline" />
            </div>
            <div style="font-size: 12px">新建系统文档</div>
          </div>
          <div v-if="editSystemInputVisible" class="edit_system_input">
            <div class="edit_system_top">
              <div style="display: flex; align-items: center; color: #000000a6;">
                <i class="el-icon-edit-outline" />
                <span style="font-size: 12px;">输入系统名称</span>
              </div>
              <i class="el-icon-close" @click="editSystemInputVisible = false" />
            </div>
            <div style="margin-top: 14px;">
              <el-input v-model="groupName" v-focus size="mini" placeholder="回车确认提交" @keyup.enter.native="menuGroupAdd" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="editSystemMenu_wrap">
      <editSystemMenu v-if="editSystemMenuVisible" ref="editSystemMenu" :curr-system="currSystem" />
    </div>
  </div>
</template>

<script>
import { menuGroupVoList, menuGroupAdd, menuGroupDelete } from '@/api/golangApi/menuGroup'
import editSystemMenu from './components/editSystemMenu'
export default {
  name: 'Systems',
  components: {
    editSystemMenu
  },
  data() {
    return {
      systemList: [],
      editSystemInputVisible: false,
      editSystemMenuVisible: false,
      groupName: '',
      currSystem: {}
    }
  },
  created() {
    this.initData()
  },
  methods: {
    initData() {
      this.menuGroupVoList()
    },
    async menuGroupVoList() {
      try {
        const res = await menuGroupVoList()
        if (res.code === 200) {
          this.systemList = res.data
        }
      } catch (error) {
        console.log(error)
      }
    },
    async menuGroupAdd() {
      try {
        const res = await menuGroupAdd({
          groupName: this.groupName,
          description: '',
          status: 1
        })
        if (res.code === 200) {
          await this.menuGroupVoList()
          this.groupName = ''
          this.editSystemInputVisible = false
        }
      } catch (error) {
        console.log(error)
      }
    },
    systemDeleteClick(item) {
      const that = this
      this.$confirm({
        title: `确认删除【${item.groupName}】吗`,
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          that.menuGroupDelete(item)
        },
        onCancel() {
          console.log('Cancel')
        }
      })
    },
    async menuGroupDelete(item) {
      try {
        const res = await menuGroupDelete({
          id: item.id
        })
        if (res.code === 200) {
          this.menuGroupVoList()
          this.$message.success(`【${item.groupName}】删除成功`)
        }
      } catch (error) {
        console.log(error)
      }
    },
    async systemItemClick(item) {
      this.currSystem = item
      this.editSystemMenuVisible = true
      this.$nextTick(() => {
        this.$refs.editSystemMenu.initData(item)
      })
    },
    systemDocumenyJump(item) {
      const jumpUrl = `${window.location.origin}/#/login?uuid=${item.uuid}`
      this.copyTextToClipboard(jumpUrl)
      window.open(jumpUrl, '_blank')
    },
    // 创建一个函数用于复制文本到剪贴板
    copyTextToClipboard(text) {
      var textArea = document.createElement('textarea')
      textArea.value = text
      document.body.appendChild(textArea)
      textArea.select()
      document.execCommand('copy')
      document.body.removeChild(textArea)
    }
  }
}
</script>

<style lang="scss" scoped>
.document_wrap {
  height: 100%;
  overflow: hidden;
  display: flex;
  .editSystemMenu_wrap {
    flex: 1;
  }
  .system_list_wrap {
    background-color: #fff;
    padding: 16px;
    width: 500px;
    min-width: 500px;
    height: 100%;
    overflow-y: auto;
    border-right: 1px solid #eee;
  }
  .system_list {
    display: flex;
    flex-wrap: wrap;
  }
  .system_item.new_system_item {
    position: relative;
    justify-content: center;
    align-items: center;
    color: #c5c5c5;
    transition: all 0.3s ease;
    border: 1px dashed #eee;
    box-shadow: none;
    cursor: pointer;
    &:hover {
      transform: translateY(0px);
      border: 1px solid #23a7fa;
      color: #23a7fa;
    }
  }
  .edit_system_input {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #fff;
    color: #000;
    padding: 10px;
    border: 1px solid #23a7fa;
    border-radius: 4px;
    &:hover {
      border: 0 solid #23a7fa;
    }
    .edit_system_top {
      display: flex;
      justify-content: space-between;
    }

  }
  .system_item_close {
    position: absolute;
    transition: all 0.3s ease;
    right: -12px;
    top: -12px;
    color: #fff;
    cursor: pointer;
  }
  .system_item {
    position: relative;
    width: 216px;
    height: 100px;
    display: flex;
    flex-direction: column;
    border-radius: 8px;
    margin-bottom: 14px;
    margin-right: 16px;
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 1px 1px hsl(0deg 0% 0% / 0.075),
      0 2px 2px hsl(0deg 0% 0% / 0.075), 0 4px 4px hsl(0deg 0% 0% / 0.075);
    transition: all 0.3s ease;
    &:hover {
      transform: translateY(-1px);

    }
    &:last-child {
      margin-right: 0;
    }
    .system_item_content {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 16px;
      background: linear-gradient(to bottom right, #23a7fa, #3d6bfd);
      color: #fff;
      &:hover {
        .system_item_close {
        top: 4px;
        right: 4px;
        transform: rotate(-90deg);
      }
      }
    }
    .system_item_operate {
      height: 30px;
      background-color: #fff;
      width: 100%;
      display: flex;
      align-items: center;
      .operate_item {
        box-sizing: border-box;
        width: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 12px;
        cursor: pointer;
      }
    }
  }
}
</style>
